#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "Head/error.h"

#define MAXLINE 8192

/*
void err_sys(const char * info, int exit_ok=1) {
    fprintf(stdin, "%s", info);
    if(exit_ok == 1)
        exit(EXIT_FAILURE);
}

void err_ret(const char * error, const char * info) {
    fprintf(stdin, error, info);
}
*/

int main(int argc, char * argv[]) {
	char	buf[MAXLINE];
	pid_t	pid;
	int		status;

	printf("%% ");
	while(fgets(buf, MAXLINE, stdin) != NULL) {
		buf[strlen(buf) - 1] = 0; /* replace newline with null*/

		if((pid = fork()) < 0)
			err_sys("fork error.");
		else if(pid == 0) { /* child */
			execlp(buf, buf, (char *)0);
			err_ret("couldn't execute: %s", buf);
			exit(27);
		}

		/* parent */
		if((pid = waitpid(pid, &status, 0)) < 0)
			err_sys("Waitpid error.");
		printf("%% ");
	}

	return 0;
}
